#if NET40

using System;
using System.Linq;
using System.Dynamic;
using System.Collections;

namespace Caramel
{
	public class DynamicDictionary : DynamicObject//, IDictionary
	{
		private readonly Hashtable _dictionary;

		public DynamicDictionary(Hashtable dictionary)
		{
			_dictionary = dictionary;
		}

		public override bool TryGetMember(GetMemberBinder binder, out object result)
		{
			result = _dictionary[binder.Name];

			return _dictionary.ContainsKey(binder.Name);
		}

		public override bool TrySetMember(SetMemberBinder binder, object value)
		{
			_dictionary[binder.Name] = value;

			return true;
		}

		public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
		{
			if (indexes == null)
			{
				throw new ArgumentNullException("indexes");
			}

			if (indexes.Length != 1)
			{
				throw new ArgumentException("", "indexes");
			}

			result = _dictionary[indexes[0]];

			return result != null;
		}

		public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
		{
			if (indexes == null)
			{
				throw new ArgumentNullException("indexes");
			}

			if (indexes.Length != 1)
			{
				throw new ArgumentException("", "indexes");
			}

			_dictionary[indexes[0]] = value;

			return true;
		}
	}
}

#endif
